package logic.gridNodes;

import eduni.simjava.Sim_entity;
import gridsim.Machine;
import gridsim.MachineList;
import gridsim.PE;
import gridsim.PEList;
import gridsim.ResourceCharacteristics;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.Vector;
import logic.gridSimInterface.resource.ResourceCharacteristicExt;
import logic.topology.NetworkLink;
import logic.topology.NetworkNode;

/**
 * Zdroj moze byt spojeny iba s jednym lokalnym routerom
 */
public class GridResource extends NetworkNode {
    
    //load parametre - hodnota z intervalu (0,1) otvoreny
    //peakLoad - the load during peak times
    private double peakLoad;
    private double offPeakLoad;
    private double holidayLoad;
    //casova zona - zrejme stacia hodnoty 0, 1, 2, ..., 23
    private double timeZone;
    //naklady na sekundu vypoctu - v euro :)
    private double costPerSec;
    //poskytovany operacny system - musi byt != ""
    private String os;
    //poskytovana architektura - Sun, Intel, ... - musi byt != ""
    private String architecture;
    //zoznam frekvencii procesorov v MIPS (million instructions per second)
    //nesmie byt prazdny a ziadna hodnota nesmie byt 0
    private Vector<Integer> processors;
    
    private static int count = 0;
    private static final String _obmedzenie = 
            "Resource must be connected to one local router!";

    public GridResource() throws Exception {
        super("GridResourceDefault123456789xtea",0,0);
        count++;
        setName("Resource" + count);
        
        setPeakLoad(0.5);
        setOffPeakLoad(0.1);
        setHolidayLoad(0.01);
        setTimeZone(1);
        setCostPerSec(50);
        setOs("Linux");
        setArchitecture("Intel");
        Vector<Integer> p = new Vector<Integer>();
        p.add(50);
        p.add(100);
        setProcessors(p);
    }

    /**
     * <font face="">vracia null, ak je mozne vytvorit novu linku z daneho uzla
     * (este 
     *    sa nevie, aky bude koniec linky). Inak vracia dovod, preco sa nemoze 
     *    vytvorit
     * </font>
     */
    public String canCreateNewLink() {
        if (getLinks().size() == 0)
            return null;
        else
            return new String(_obmedzenie);
    }

    /**
     * Vracia null, ak moze vytvorit spojenie s danym elementom. Inak vracia
     * dovod, 
     *    preco nie. Kontroluje aj, ci uz linka s danym elementom neexistuje.
     */
    public String canCreateNewLink(NetworkNode end) {
        if ((getLinks().size() == 0) && (end instanceof GridRouterLocal))
            return null;
        else
            return new String(_obmedzenie);
    }
    
    public double getPeakLoad() {
        return peakLoad;
    }

    public void setPeakLoad(double peakLoad) {
        this.peakLoad = peakLoad;
    }

    public double getOffPeakLoad() {
        return offPeakLoad;
    }

    public void setOffPeakLoad(double offPeakLoad) {
        this.offPeakLoad = offPeakLoad;
    }

    public double getHolidayLoad() {
        return holidayLoad;
    }

    public void setHolidayLoad(double holidayLoad) {
        this.holidayLoad = holidayLoad;
    }

    public double getTimeZone() {
        return timeZone;
    }

    public void setTimeZone(double timeZone) {
        this.timeZone = timeZone;
    }

    public double getCostPerSec() {
        return costPerSec;
    }

    public void setCostPerSec(double cost) {
        this.costPerSec = cost;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    public String getArchitecture() {
        return architecture;
    }

    public void setArchitecture(String architecture) {
        this.architecture = architecture;
    }

    public LinkedList<Sim_entity> createGridSimEntities(String globalBrokerName, boolean trace_flag) throws Exception {
        gridsim.GridResource resource;
        
	PEList peList = new PEList(); 
        for (int i = 0; i < processors.size(); i++) {
            peList.add(new PE(i, processors.get(i)));
        }
        
	MachineList mList = new MachineList();
        mList.add(new Machine(0,peList));   //0 - index pocitaca - je iba jeden => 0
        
        ResourceCharacteristicExt resConfig = new ResourceCharacteristicExt(
                getArchitecture(), getOs(), mList, ResourceCharacteristics.TIME_SHARED,
                getTimeZone(), getCostPerSec());

        LinkedList weekends = new LinkedList();
        weekends.add(new Integer(Calendar.SATURDAY));
        weekends.add(new Integer(Calendar.SUNDAY));
        LinkedList holidays = new LinkedList();
	            
        resource =  new gridsim.GridResource(
                getName(),
                getLinks().iterator().next().createSimpleLink(),
                0,  //seed
                resConfig,
                getPeakLoad(),
                getOffPeakLoad(),
                getHolidayLoad(),
                weekends,
                holidays
                );
        
        LinkedList<Sim_entity> result = new LinkedList<Sim_entity>();
        result.add(resource);
        return result;
    }

    public Vector<Integer> getProcessors() {
        return new Vector<Integer>(processors);
    }

    public void setProcessors(Vector<Integer> processors) {
        this.processors = new Vector<Integer>(processors);
    }
}
